package com.ruoyi.test.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.test.domain.bo.HnProductBo;
import com.ruoyi.test.domain.vo.HnProductVo;
import com.ruoyi.test.domain.HnProduct;
import com.ruoyi.test.mapper.HnProductMapper;
import com.ruoyi.test.service.IHnProductService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品管理Service业务层处理
 *
 * @author wpq
 * @date 2024-04-10
 */
@RequiredArgsConstructor
@Service
public class HnProductServiceImpl implements IHnProductService {

    private final HnProductMapper baseMapper;

    /**
     * 查询商品管理
     */
    @Override
    public HnProductVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品管理列表
     */
    @Override
    public TableDataInfo<HnProductVo> queryPageList(HnProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HnProduct> lqw = buildQueryWrapper(bo);
        Page<HnProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品管理列表
     */
    @Override
    public List<HnProductVo> queryList(HnProductBo bo) {
        LambdaQueryWrapper<HnProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HnProduct> buildQueryWrapper(HnProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HnProduct> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), HnProduct::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), HnProduct::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getPrice()), HnProduct::getPrice, bo.getPrice());
        lqw.eq(bo.getQuantity() != null, HnProduct::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), HnProduct::getCategory, bo.getCategory());
        return lqw;
    }

    /**
     * 新增商品管理
     */
    @Override
    public Boolean insertByBo(HnProductBo bo) {
        HnProduct add = BeanUtil.toBean(bo, HnProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品管理
     */
    @Override
    public Boolean updateByBo(HnProductBo bo) {
        HnProduct update = BeanUtil.toBean(bo, HnProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HnProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
